package com.oocl.webmvc.controller;

import java.util.List;
import java.util.UUID;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.webmvc.dao.impl.CustomerDaoImpl;
import com.oocl.webmvc.pojo.Customer;
import com.oocl.webmvc.service.CustomerService;

/**
 * @版本说明：1、 相比之前多了 method = RequestMethod.PUT, consumes="application/json"
 * 
 * 			2、写完后台，用postman来测试
 * 
 * 			
 * @description: 该controller 为单例模式，只初始化一次
 * @author  XIEMI2
 * @date Aug 7, 2017 4:22:31 PM
 * @version 2.0
 */
@Controller
public class CustomerController {

	@Autowired
	private CustomerService cService;

	
	
	public CustomerController() {
		super();
		System.out.println("controller start........");
	}
	/**
	 * 用 postman 进行测试addCustomer的时候，注意这里是用请求参数  cust的，在那里也要协商
	 * @param cust
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addCustomer")
	public Customer addCustomer(@RequestBody Customer cust){
		
		cust.setId(UUID.randomUUID().toString());
		System.out.println("cust..."+cust.toString());
		cService.addCustomer(cust);
		return cust;  
	}
	
	@ResponseBody
	@RequestMapping(value = "/delCustomer")
	public String delCustomer(String customerId){
		System.out.println("当前需要删除的Id为 "+customerId);
		String re = cService.delCustomer(customerId);
		System.out.println("删除后还剩下的customers 信息为： "+cService.findAllCustomers());
		return re;
	}
	
	@ResponseBody
	@RequestMapping("/updateCustomer")
	public List<Customer> updateCustomer(@RequestBody Customer cust){
		System.out.println("需要更新的cust信息： "+cust.toString());
		cService.updateCustomer(cust);
		return cService.findAllCustomers();
	}
	
	@RequestMapping("/showAddCustomerPage")
	public String showAddCustomerPage(){
		return "addCustomer";
	}
	
	@ResponseBody
	@RequestMapping("/loadAllCustomers")
	public List<Customer> loadAllCustomers(){
		System.out.println("进来 loadAll");
		System.out.println("当前有的： "+cService.findAllCustomers());
		return cService.findAllCustomers();
	}
}


